package com.libing.web;

import com.libing.base.BaseController;
import com.libing.entity.InvoiceInformation;
import com.libing.enums.ResultInfoEnum;
import com.libing.enums.StateEnum;
import com.libing.service.InvoiceInformationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 开票信息 Controller
 * @Auther: LEE
 * @Date: Create in 2018/6/29 21:59
 */
@Controller
@RequestMapping("/invoice")
public class InvoiceInformationController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private InvoiceInformationService invoiceInformationService;

    @PostMapping(value = "/", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public StateEnum insert(InvoiceInformation invoiceInformation) {
        logger.debug("新增发票资料： " + invoiceInformation.toString());
        invoiceInformation.setCreateId(getLoginUser().getId());
        invoiceInformation.setCreateName(getLoginUser().getName());
        StateEnum result = invoiceInformationService.insert(invoiceInformation);
        System.out.println("**InvoiceInformationController.insert.33==> " + result);
        return result;
    }

    @PutMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
    public String update(@PathVariable("id") Long id, InvoiceInformation invoiceInformation, Model model) {
        logger.debug("修改发票信息ID:" + id + "，信息： " + invoiceInformation.toString());
        if (!invoiceInformation.getId().equals(id)) {
            model.addAttribute("result", ResultInfoEnum.DISAGREE.getInfo());
        } else {
            StateEnum result = invoiceInformationService.update(invoiceInformation);
            model.addAttribute("result", result.getStateInfo());
        }
        return "redirect:/invoice/list";
    }

    @DeleteMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
    public String delete(@PathVariable("id") Long id, Model model) {
        logger.debug("删除发票信息： id = " + id);
        StateEnum result = invoiceInformationService.delete(id);
        model.addAttribute("result", result.getStateInfo());
        return "redirect:/invoice/list";
    }

    @PostMapping(value = "/selList", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<InvoiceInformation> selectAll() {
        return invoiceInformationService.selectAll();
    }

}
